package com.queryBuilder.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.resource.cci.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.queryBuilder.builer.BuilderBasic;
import com.queryBuilder.util.QueryType;
import com.sun.xml.bind.v2.schemagen.xmlschema.Wildcard;

/**
 * 初始化和刷新左侧数据列
 * 
 * @author pengfei
 * 
 */
public class DataRefreshServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1906658459314537297L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = null;
		String action = req.getParameter("action");
		BuilderBasic builderBasic = new BuilderBasic();
		String returnValue = "";
		JSONArray jsonArray = null;
		try {
			if (action.trim().equals(QueryType.TEMPLATE)) {
				/**
				 * 获取所有模板
				 * 
				 * return defaultData;
				 */
				jsonArray = builderBasic.getAllQueryTemplate();
				returnValue = jsonArray.toString();

			} else if (action.trim().equals(QueryType.ALLCLASSES)) {
				// 编辑查询模板
				String templateName = req.getParameter("templateName");
				templateName = URLDecoder.decode(templateName,"utf-8");
				JSONObject templateCls = builderBasic
						.getEntitiesNameByTemplateName(templateName);
				int count = templateCls.getInt("count");
				List<String> entitis = new ArrayList<String>();
				JSONObject targeJson= new JSONObject();
				if(count>0){
					JSONArray jsonArray2  = templateCls.getJSONArray("entities");
					targeJson = templateCls.getJSONObject("data");
					for(int i = 0 ;i<jsonArray2.length();i++){
						entitis.add(jsonArray2.getString(i));
					}
				}
				
				JSONObject sourceJson = builderBasic.getAllEntityName(count,entitis);
				if(sourceJson == null)
					returnValue = "{'source':{},'targe':{}}";
				else{
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("source", sourceJson);
					jsonObject.put("targe", targeJson);
					returnValue = jsonObject.toString();
				}
			}else if (action.equals(QueryType.ADDTEMPLATE)){
				/**
				 * checkResult.entities = entities;
	checkResult.templateName = templateName;
				 */
				String templateName = req.getParameter("templateName");
				String [] clsName = req.getParameter("entities").split(",");
				//新增模板
				boolean saveType = builderBasic.addTemplate(templateName,clsName,null);
				returnValue  = "{'saveType':'"+String.valueOf(saveType)+"'}";
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
		System.out.println(returnValue);
		out = response.getWriter();
		out.write(returnValue);
	}
}
